﻿// Include the namespace required to use Unity UI
using UnityEngine;
using UnityEngine.Assertions;

public class PlayerController : MonoBehaviour {
	private Transform tr;

	// 服务器状态，适用于非主角
	private Vector3 serverPos;
	private Quaternion serverRot;

	// 主角才有的控制器
	private MyPlayerController myPlayerCtrl = null;

	// At the start of the game..
	void Start()
	{
		tr = transform;
	}

    private void Update()
    {
		if (myPlayerCtrl != null)
			return;  // 主角主动更新，不要听服务器的

		float t = Time.deltaTime * 5;
		tr.position = Vector3.Lerp(tr.position, serverPos, t);
		tr.rotation = Quaternion.Slerp(tr.rotation, serverRot, t);
    }

    // When this game object intersects a collider with 'is trigger' checked, 
    // store a reference to that collider in a variable named 'other'..
    void OnTriggerEnter(Collider other) 
	{
		// ..and if the game object we intersect has the tag 'Pick Up' assigned to it..
		if (!other.gameObject.CompareTag("Pick Up"))
			return;

		// Make the other game object (the pick up) inactive, to make it disappear
		other.gameObject.SetActive (false);
		if (myPlayerCtrl)
			myPlayerCtrl.IncCount();
	}

	// 由服务器更新位置，适用于非主角
	public void SetServerPos(Vector3 pos)
	{
		serverPos = pos;
	}

	// 由服务器更新转向，适用于非主角
	public void SetServerRot(Quaternion rot)
    {
		serverRot = rot;
    }

	public void SetIsMyPlayer()
    {
		myPlayerCtrl = gameObject.AddComponent<MyPlayerController>();
		Assert.IsNotNull(myPlayerCtrl);
	}
}